﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class FormGames : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString.AllKeys.Contains("show"))
            {
                string showType = Request.QueryString["show"];
                if (showType == "new")
                {
                    LoadNewGames();
                    htitle.InnerText = "New Games";
                }
                else if (showType == "top")
                {
                    LoadTopGames();
                    htitle.InnerText = "Top Games";
                }
            }
        }

        private void LoadNewGames()
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                GridGames.DataSource = dataContext.GetNewGames(false);
                GridGames.DataBind();
            }
        }

        private void LoadTopGames()
        {
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                GridGames.DataSource = dataContext.GetTopGames(false);
                GridGames.DataBind();
            }
        }

        protected void lnkGamesPlay_Click(object sender, EventArgs e)
        {
            LinkButton lnkGame = sender as LinkButton;
            int gameId = Convert.ToInt32(lnkGame.Attributes["GameID"]);
            string title = lnkGame.Text;
            string imageUrl = "";

            if (Request.Cookies.AllKeys.Contains("fguid"))
            {
                using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
                {
                    imageUrl = dataContext.GetGameImage(gameId);
                }

                string[] splitStr = Request.Cookies["fguid"].Value.Split('/');
                long userId = Convert.ToInt64(splitStr[0]);
                string username = splitStr[1];
                Common.AddMemberActivity(username, userId, title, gameId, imageUrl, ActivityType.GamePlaying);
            }

            Response.Redirect(string.Format("PlayGame.aspx?gameid={0}", gameId));
        }
    }
}